//采购单按钮
function clickCompanyPurchase(){
	var obj=$("#bag").html("");
	obj.load("companyPurchaseMapping",function(response,status,xhr){
		var data="pages="+"&purchaseNo=";
		showAllCompanyPurchaseTableInfo(data);
	})
}

//显示客户订单详情信息
function showAllCompanyPurchaseTableInfo($data){
	var obj=$("#companyPurchaseTable tbody").html("");
	//获取路径
	var url="/trade-wzj-web/tests/getAllCompanyPurchaseInfo";
	//获取参数
	var data=$data;
	//定义字符串拼接
	var str="";
	//调用提交方法
	var result = postMethod(url,"POST",data);
	//获取集合
	var list=result.list;
	console.info(list);
	if(list.length==0){
		alert("对不起，查询数据不存在");
		return;
	}
	if(list.length>0){
		
		for(var i=0;i<list.length;i++){
			str=str+"<tr>" +
						"<td><input type='hidden' id='companyPurchaseId' value='"+list[i].id+"'>"+list[i].purchaseNo+"</td>"+
						"<td>"+list[i].purchaseDate+"</td>"+
						"<td>"+list[i].purchaseDelivery+"</td>"+
						"<td>"+list[i].orderTotal+"</td>"+
						"<td>"+list[i].supplierInfo.supplierName+"</td>"+
						"<td>"+list[i].state.stateInfo+"</td>"+
						"<td>"+list[i].companyRemark+"</td>"+
						"<td><a href='#' onclick='bookMoney("+list[i].id+","+list[i].supplierInfo.supplierInfoID+")'><span class='glyphicon glyphicon-check'></span></a>&nbsp;<a href='#' onclick=checkCompanyDetails("+list[i].id+","+list[i].supplierInfo.supplierInfoID+")><span class='glyphicon glyphicon-search'></span></a></td>"+
						
					"</tr>";
			
		}
		
		//da.append(d);
	}
	
	var page=$("#companyPurchasePage").html("");
	//调用分页代码
	var va=companyPurchasePages(result);
	page.append(va);
	obj.append(str);
}


//生成公司采购单按钮事件
function addCompanyOrder(){
	var condition=document.getElementsByName("clientOrderCheck");
	//获取选中信息的条件
	var checks=[];
	for(var i=0;i<condition.length;i++){
		if(condition[i].checked==true){
			checks.push(condition[i].value);
		}
	}
	var data="clientQuotaID="+checks;
	var url="/trade-wzj-web/tests/getCompanyOrderMethod";
	//提交信息
	var result=postMethod(url,"POST",data);
	var list=result;
	console.info(list);
	var str="";
	if(list.length==0){
		return;
	}
	var obj=$("#saveCompanyOrderTable tbody").html("");
	for(var i=0;i<list.length;i++){
		str=str+"<tr>" +
					"<td><input name='clientOrderId' type='hidden' class='form-control' value='"+list[i].clientOrderInfo.id+"'><input type='text' class='form-control' value='"+list[i].productInfoQuota.productName+"'></td>"+
					"<td><input name='productInfoQuotaID' type='hidden' class='form-control' value='"+list[i].productInfoQuota.productInfoQuotaID+"'><input name='unit' type='text' class='form-control' value='"+list[i].unit+"'></td>"+
					"<td><input name='supplierInfoID' type='hidden' class='form-control' value='"+list[i].productInfoQuota.supplierQuota.supplierInfo.supplierInfoID+"'><input name='productNum' type='text' class='form-control' value='"+list[i].productNum+"'></td>"+
					"<td><input name='productPrice' type='text' class='form-control' value='"+list[i].productInfoQuota.productPrice+"'></td>"+
					"<td><input name='totalPrice' type='text' class='form-control' value='"+(list[i].productNum*list[i].productInfoQuota.productPrice)+"'></td>"+
					"<td><input type='text' class='form-control' value='"+list[i]+"'></td>"+
				"</tr>";
	}
	obj.append(str);
	$("#saveCompanyOrderModal").modal("show");
}

//点击生成公司采购单
function saveCompanyOrderBtn(){
	var addCompanyOrderForms = $("#addCompanyOrderForms").Validform({
		tiptype:1
	});
	var bool = addCompanyOrderForms.check();
	if(!bool){
		$("#saveCompanyOrderId").popover("toggle");
		return;
	}
	var formData = $("#addCompanyOrderForms").serialize();
	//获取路径
	var url="/trade-wzj-web/tests/addCompanyPurchase";
	var data="purchaseDate="+$("#purchaseDate").val()+"&purchaseDelivery="+$("#purchaseDelivery").val()+"&"+formData;
	var result=postMethod(url,"POST",data);
	if(result){
		alert("操作成功!");
	}else{
		alert("操作失败!");
	}
	$("#saveCompanyOrderModal").modal("hide");
}




//获取页码信息
function companyPurchasePages($clientList){
	var page="";
	
	var con = $("#companyPurchaseNo").val();
	page = page+"<div class='row' style='margin:0px'>" +
	"<div class='col-md-2 col-sm-2 col-xs-2' style='padding-top:0.7rem'>"+
		"<span style='font-weight:bold'>总记录数 "+$clientList.total+"   |  总页数 "+$clientList.pages+"</span>"+
	"</div>"+
	"<div class='col-md-offset-8 col-sm-offset-8 col-xs-offset-8'>"
	+"<nav aria-label='Page navigation'>"+
"<ul class='pagination pagination-sm'>";
	if($clientList.prePage>0){
		page = page+"<li><a href='#' onclick='makeCompanyPurchasePages("+$clientList.firstPage+",\""+con+"\")'>首页</a></li>"+
		"<li><a href='#' onclick='makeCompanyPurchasePages("+$clientList.prePage+",\""+con+"\")'>上一页</a></li>";
				
	}
	if($clientList.nextPage<=$clientList.lastPage && $clientList.nextPage>0){
		page = page+"<li><a href='#' onclick='makeCompanyPurchasePages("+$clientList.nextPage+",\""+con+"\")'>下一页</a></li>"+
		"<li><a href='#' onclick='makeCompanyPurchasePages("+$clientList.lastPage+",\""+con+"\")'>末页</a></li>";
	}
	page = page+"</li>"+
"</ul>"+
"</nav>"+
"</div>"+
"</div>";
	return page;
	
}

//分页功能
function makeCompanyPurchasePages($page,$object){
	var data="pages="+$page+"&purchaseNo="+$object;
	showAllCompanyPurchaseTableInfo(data);
}


//公司采购单查询
function checkCompanyPurchaseClick(){
	//获取查询条件
	var data="pages="+"&purchaseNo="+$("#companyPurchaseNo").val();
	showAllCompanyPurchaseTableInfo(data);
}